package com.example.tangjiangxu.rotatepicbrowserdemo;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private RelativeLayout layout;
    private ListView lvPic;
    private ImageView ivpic;
    private PictureAdapter adapterPic;
    private List<Picture> pictureList = new ArrayList<Picture>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //对图片列表数据进行初始化操作
        initPics();

        layout = (RelativeLayout) findViewById(R.id.layout);
        lvPic = (ListView) findViewById(R.id.lv);
        ivpic = (ImageView) findViewById(R.id.picture);

        adapterPic = new PictureAdapter(this, 0, pictureList);
        lvPic.setAdapter(adapterPic);

        lvPic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ivpic.setImageResource(pictureList.get(position).getResource());
                float centerX = layout.getWidth() / 2f;
                float cneterY = layout.getHeight() / 2f;
                //构建3D旋转动画对象，旋转角度为0到90度，这使得ListView从可见变为不可见
                final Rotate3dAnimation rotate3dAnimation = new Rotate3dAnimation(0, 90, centerX, cneterY, 310.0f, true);
                //动画支持时间500毫秒
                rotate3dAnimation.setDuration(500);
                //动画完成后保持完成的状态
                rotate3dAnimation.setFillAfter(true);
                rotate3dAnimation.setInterpolator(new AccelerateInterpolator());
                //设置动画的监听器
                rotate3dAnimation.setAnimationListener(new TrunToImageView());
                layout.startAnimation(rotate3dAnimation);
            }
        });

        ivpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float centerX = layout.getWidth() / 2f;
                float cneterY = layout.getHeight() / 2f;
                //构建3D旋转动画对象，旋转角度为360到270度，这使得ImageView从可见到不可见，并且旋转方向为反方向
                final Rotate3dAnimation rotate3dAnimation = new Rotate3dAnimation(360, 270, centerX, cneterY, 310.0f, true);
                //动画支持时间500毫秒
                rotate3dAnimation.setDuration(500);
                //动画完成后保持完成的状态
                rotate3dAnimation.setFillAfter(true);
                rotate3dAnimation.setInterpolator(new AccelerateInterpolator());
                //设置动画的监听器
                rotate3dAnimation.setAnimationListener(new TrunToListView());
                layout.startAnimation(rotate3dAnimation);
            }
        });

    }

    /**
     * 初始化图片列表数据
     */
    private void initPics() {
        Picture p1 = new Picture("Bird", R.mipmap.t1);
        pictureList.add(p1);
        Picture p2 = new Picture("Winter", R.mipmap.t2);
        pictureList.add(p2);
        Picture p3 = new Picture("Autumn", R.mipmap.t3);
        pictureList.add(p3);
        Picture p4 = new Picture("Greate Wall", R.mipmap.t1);
        pictureList.add(p4);
    }

    /**
     * 注册在ListView点击动画中的动画监听器，用于完成ListView的后续动画
     */
    class TrunToImageView implements Animation.AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        /**
         * 当ListView的动画完成后，还需要再开启ImageView的动画，让Imageview从不可见变为可见
         * @param animation
         */
        @Override
        public void onAnimationEnd(Animation animation) {
            float centerX = layout.getWidth() / 2;
            float centerY = layout.getHeight() / 2;
            //将listview隐藏
            lvPic.setVisibility(View.GONE);
            //将ImageView显示
            ivpic.setVisibility(View.VISIBLE);
            ivpic.requestFocus();
            //构建3D旋转动画对象，旋转角度为270到360，这使得ImageView将会从不可见变为可见
            final Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY, 310.0f, false);
            //动画支持时间500毫秒
            rotation.setDuration(500);
            //动画完成后保持完成的状态
            rotation.setFillAfter(true);
            rotation.setInterpolator(new AccelerateInterpolator());
            layout.startAnimation(rotation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }

    /**
     * 注册在ImageView点击动画中的动画监听器，用于完成ListView的后续动画
     */
    class TrunToListView implements Animation.AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        /**
         * 当ListView的动画完成后，还需要再开启ImageView的动画，让Imageview从不可见变为可见
         * @param animation
         */
        @Override
        public void onAnimationEnd(Animation animation) {
            float centerX = layout.getWidth() / 2;
            float centerY = layout.getHeight() / 2;
            //将listview显示
            lvPic.setVisibility(View.VISIBLE);
            //将ImageView隐藏
            ivpic.setVisibility(View.GONE);
            //ivpic.requestFocus();
            //构建3D旋转动画对象，旋转角度为90到0，这使得ListView将会从不可见变为可见，从而回到原点
            final Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false);
            //动画支持时间500毫秒
            rotation.setDuration(500);
            //动画完成后保持完成的状态
            rotation.setFillAfter(true);
            rotation.setInterpolator(new AccelerateInterpolator());
            layout.startAnimation(rotation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }
}
